﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;

namespace Runda.CMS.Portal.SysManage
{
    public partial class UserList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUserList(1);
            }
        }
        private void BindUserList(int pageindex)
        {
            //List<SqlDbParameter> parms = new List<SqlDbParameter>();
            string Whereclause = "";
            if (txtAccount.Text.Trim() != "")
            {
                Whereclause += " and (Account like '%" + txtAccount.Text.Trim() + "%')";
            }

            if (txtUserName.Text.Trim() != "")
            {
                Whereclause += " and (UserName like '%" + txtUserName.Text.Trim() + "%')";
            }
            if (!includeDel.Checked)
            {
                Whereclause += " and (Deleted =0 )";
            }

            int RecordCount = 0;
            //List<SysUserEntity> list = new SysUserBll().GetAll<SysUserEntity>(Whereclause, "Account asc", AspNetPager1.PageSize, pageindex, out RecordCount);
            DataTable dt = new SysUserBll().getUserList(Whereclause, "Account asc", AspNetPager1.PageSize, pageindex, out RecordCount);

            RepeaterUsers.DataSource = dt;
            RepeaterUsers.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void RepeaterUsers_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string UserID;
            if (e.CommandName == "refersh")
            {
                BindUserList(1);
            }
            else
            {
                Label lblUserID = e.Item.FindControl("UserID") as Label;
                UserID = lblUserID.Text;

                if (e.CommandName == "ReStart")
                {
                    new SystemBll().ReStartUser(UserID, CurrentUserID.ToString());
                }

                if (e.CommandName == "ReSetPwd")
                {
                    new SystemBll().ReSetPwd(UserID, CurrentUserID.ToString());
                }

                BindUserList(1);
            }
        }
        protected void lnkbtnDelete_Click(object sender, EventArgs e)
        {
            if (userids.Value.Trim() != "") {
                string[] useridsArray = userids.Value.Trim().Split(new char[] { ';' });
                for (int index = 0; index < useridsArray.Length; index++) {
                    SysUserEntity entity = new SysUserBll().Get<SysUserEntity>(new Guid(useridsArray[index]));
                    new SysUserBll().LogicDelete(entity);
                    BindUserList(1);
                }
            }
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {

            BindUserList(AspNetPager1.CurrentPageIndex);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindUserList(1);
        }

        protected void RepeaterUsers_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label UserID = e.Item.FindControl("UserID") as Label;
                LinkButton lbReStart = e.Item.FindControl("lbReStart") as LinkButton;
                LinkButton lbReSetPwd = e.Item.FindControl("lbReSetPwd") as LinkButton;
                Label Status = e.Item.FindControl("Status") as Label;
                Label StatusName = e.Item.FindControl("StatusName") as Label;
                Label LastLogintime = e.Item.FindControl("LastLogintime") as Label;

                if (Status.Text == "True")
                { StatusName.Text = "已停用"; lbReStart.Visible = true; lbReSetPwd.Visible = false; }
                else
                { StatusName.Text = "使用中"; lbReStart.Visible = false; lbReSetPwd.Visible = true; }

                LastLogintime.Text = new SysUserBll().queryLastLogin(UserID.Text);

            }
        }
    }
}
